package lmycc.test.demo.config;

import lmycc.test.demo.service.MailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Enumeration;

import static org.springframework.http.HttpStatus.NOT_EXTENDED;

public class Global {


    /**
     * Created 17/3/31.
     * 全局异常处理
     */
    @ControllerAdvice
    public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
        private Logger logger = LoggerFactory.getLogger(getClass().getName());
        @Autowired
        private MailService mailService;

        /**
         * 在controller里面内容执行之前，校验一些参数不匹配啊，Get post方法不对啊之类的
         */
        @Override
        protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers, HttpStatus status, WebRequest request) {
            System.out.println("错误");
            return new ResponseEntity<>("出错了", NOT_EXTENDED);
        }

        @ExceptionHandler(value = Exception.class)
        @ResponseBody
        public String jsonHandler(HttpServletRequest request, Exception e) throws Exception {
            log(e, request);

            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            //发送邮件
            mailService.sendSimpleMail("1428180172@qq.com", "系统异常", sw.toString());

            return "发生异常";
        }

        private void log(Exception ex, HttpServletRequest request) {
            logger.error("************************异常开始*******************************");
            logger.error("请求地址：" + request.getRequestURL());
            Enumeration enumeration = request.getParameterNames();
            logger.error("请求参数");
            while (enumeration.hasMoreElements()) {
                String name = enumeration.nextElement().toString();
                logger.error(name + "---" + request.getParameter(name));
            }

            StackTraceElement[] error = ex.getStackTrace();
            for (StackTraceElement stackTraceElement : error) {
                logger.error(stackTraceElement.toString());
            }
            logger.error("************************异常结束*******************************");
        }
    }
}